package com.fr.data.condition;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.list.IntList;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.data.core.Compare;
import com.fr.report.cellElement.Formula;
import com.fr.report.core.ReportCoreConstants;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Calculator;
import com.fr.report.script.Primitive;
import com.fr.report.script.core.FArray;
import com.fr.report.script.core.parser.UtilEvalError;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CommonCondition extends AbstractCondition
{
  private String columnName;
  private Compare compare;

  public CommonCondition()
  {
  }

  public CommonCondition(String paramString, int paramInt, Object paramObject)
  {
    this(paramString, new Compare(paramInt, paramObject));
  }

  public CommonCondition(String paramString, Compare paramCompare)
  {
    this.columnName = paramString;
    this.compare = paramCompare;
  }

  public Compare getCompare()
  {
    return this.compare;
  }

  public void setCompare(Compare paramCompare)
  {
    this.compare = paramCompare;
  }

  public String getColumnName()
  {
    return this.columnName;
  }

  public void setColumnName(String paramString)
  {
    this.columnName = paramString;
  }

  public String[] getDependence()
  {
    return new String[0];
  }

  public boolean eval(Object paramObject, Calculator paramCalculator)
  {
    Compare localCompare = getCompare();
    Object localObject = localCompare.getValue();
    if ((!(localObject instanceof String)) && (!(localObject instanceof Primitive)))
    {
      localObject = paramCalculator.resolveVariable(localObject);
      if (localObject != null)
        try
        {
          localCompare = (Compare)localCompare.clone();
          localCompare.setValue(localObject);
        }
        catch (CloneNotSupportedException localCloneNotSupportedException)
        {
          FRContext.getLogger().log(Level.WARNING, localCloneNotSupportedException.getMessage(), localCloneNotSupportedException);
        }
    }
    return localCompare.eval(paramObject);
  }

  public int[] evalTableData(TableData paramTableData, int[] paramArrayOfInt, int paramInt, Calculator paramCalculator)
  {
    int l;
    int i1;
    if ((paramTableData == null) || (this.columnName == null))
      return new int[0];
    IntList localIntList = new IntList();
    int i = -1;
    try
    {
      int j = paramTableData.getColumnCount();
      for (l = 0; l < j; ++l)
        if (ComparatorUtils.tableDataColumnNameEquals(this.columnName, paramTableData.getColumnName(l)))
        {
          i = l;
          break;
        }
    }
    catch (TableDataException localTableDataException1)
    {
      FRContext.getLogger().log(Level.WARNING, localTableDataException1.getMessage(), localTableDataException1);
    }
    if (i == -1)
      return localIntList.toArray();
    int k = 0;
    try
    {
      k = paramTableData.getRowCount();
    }
    catch (TableDataException localTableDataException2)
    {
      FRContext.getLogger().log(Level.WARNING, localTableDataException2.getMessage(), localTableDataException2);
    }
    if (paramArrayOfInt == null)
      paramArrayOfInt = IntList.range(k);
    Compare localCompare = getCompare();
    if (localCompare != null)
    {
      Object localObject1 = localCompare.getValue();
      if ((!(localObject1 instanceof String)) && (!(localObject1 instanceof Primitive)))
      {
        if (localObject1 instanceof Formula)
        {
          try
          {
            localObject1 = paramCalculator.eval(((Formula)localObject1).getContent());
          }
          catch (UtilEvalError localUtilEvalError)
          {
            localObject1 = null;
          }
        }
        else if (localObject1 instanceof Parameter)
        {
          localObject1 = ((Parameter)localObject1).getValue();
          if (localObject1 instanceof JSONArray)
          {
            JSONArray localJSONArray = (JSONArray)localObject1;
            FArray localFArray = new FArray();
            int i3 = 0;
            int i4 = localJSONArray.length();
            while (i3 < i4)
            {
              try
              {
                localFArray.add(localJSONArray.get(i3));
              }
              catch (JSONException localJSONException)
              {
              }
              ++i3;
            }
            localObject1 = localFArray;
          }
        }
        else
        {
          localObject1 = paramCalculator.resolveVariable(localObject1);
        }
        if (localObject1 != null)
          try
          {
            localCompare = (Compare)localCompare.clone();
            localCompare.setValue(localObject1);
          }
          catch (CloneNotSupportedException localCloneNotSupportedException)
          {
            FRContext.getLogger().log(Level.WARNING, localCloneNotSupportedException.getMessage(), localCloneNotSupportedException);
          }
      }
      for (i1 = 0; i1 < paramArrayOfInt.length; ++i1)
      {
        int i2 = paramArrayOfInt[i1];
        if (i2 >= 0)
        {
          if (i2 >= k)
            break label442:
          try
          {
            Object localObject2 = paramTableData.getValueAt(i2, i);
            if (localCompare.eval(localObject2))
              localIntList.add(i2);
          }
          catch (TableDataException localTableDataException3)
          {
            FRContext.getLogger().log(Level.WARNING, localTableDataException3.getMessage(), localTableDataException3);
          }
        }
      }
    }
    label442: return localIntList.toArray();
  }

  public boolean equals(Object paramObject)
  {
    if ((paramObject == null) || (!(paramObject instanceof CommonCondition)))
      return false;
    CommonCondition localCommonCondition = (CommonCondition)paramObject;
    return ((ComparatorUtils.equals(localCommonCondition.getColumnName(), getColumnName())) && (ComparatorUtils.equals(localCommonCondition.getCompare(), getCompare())));
  }

  public String toString()
  {
    return getColumnName() + " " + ReportCoreConstants.conditionToString(getCompare());
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      String str = paramXMLableReader.getTagName();
      if (("Compare".equals(str)) || ("Condition".equals(str)))
      {
        this.compare = new Compare(0, "");
        paramXMLableReader.readXMLObject(this.compare);
      }
      else if ("CNAME".equals(str))
      {
        this.columnName = paramXMLableReader.getElementValue();
      }
      else if ("SelectionColumn".equals(str))
      {
        this.columnName = paramXMLableReader.getAttr("name");
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    if (StringUtils.isNotBlank(this.columnName))
    {
      paramXMLPrintWriter.startTAG("CNAME").textNode(this.columnName);
      paramXMLPrintWriter.end();
    }
    if (getCompare() != null)
      getCompare().writeXML(paramXMLPrintWriter);
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    CommonCondition localCommonCondition = (CommonCondition)super.clone();
    if (this.compare != null)
      localCommonCondition.compare = ((Compare)this.compare.clone());
    return localCommonCondition;
  }

  public JSONObject createJSON()
    throws Exception
  {
    return null;
  }

  public void parseJSON(JSONObject paramJSONObject)
    throws Exception
  {
    if (paramJSONObject.get("columnname") != null)
      setColumnName(paramJSONObject.getString("columnname"));
    if (paramJSONObject.get("compare") != null)
    {
      Compare localCompare = new Compare();
      localCompare.parseJSON(paramJSONObject.getJSONObject("compare"));
      setCompare(localCompare);
    }
  }
}